package com.xbongbong.paas.script.controller;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.admin.pojo.vo.MirrorUpdateVO;
import com.xbongbong.admin.service.MirrorConfigService;
import com.xbongbong.paas.manager.feign.MirrorFeignClent;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.script.pojo.dto.MirrorDeleteAttrDTO;
import com.xbongbong.pro.script.pojo.dto.MirrorUpdateAttrDTO;
import com.xbongbong.pro.script.pojo.dto.MirrorUpdateDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Locale;

/**
 * @auther: xingxing.xiao
 * @date: 3/3/2020 14:22
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mirror")
public class MirrorUpdateController {

    private static final Logger LOG = LoggerFactory.getLogger(MirrorUpdateController.class);

    @Resource
    private ProBaseConfig proBaseConfig;
    @Resource
    private MirrorFeignClent mirrorFeignClent;
    @Resource
    private MirrorConfigService mirrorConfigService;

    /**
     * 更新镜像配置信息(图标，简介)
     * @author xingxing.xiao
     */
    @RequestMapping(value = "/updateMirrorInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateMirrorInfo(@RequestBody @Valid MirrorUpdateDTO mirrorUpdateDTO) throws Exception {
        if (!proBaseConfig.getXbbCode().equals(mirrorUpdateDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }

        Locale locale = LocaleContextHolder.getLocale();
        String lang = locale.toString();
        XbbResponse<MirrorUpdateVO> response = mirrorFeignClent.updateMirrorInfoScript(mirrorUpdateDTO, lang);
        MirrorUpdateVO result = response.getResult();
        JSONObject obj = new JSONObject();
        obj.put("successCount", "成功更新记录：" + result.getCount());
        return obj.toJSONString();
    }

    /**
     * 处理解释字段的属性值
     * @param mirrorUpdateAttrDTO
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateMirrorAttr", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateMirrorAttr(@RequestBody @Valid MirrorUpdateAttrDTO mirrorUpdateAttrDTO) throws Exception {
        if (!proBaseConfig.getXbbCode().equals(mirrorUpdateAttrDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }

        Integer integer = mirrorConfigService.updateMirrorAttr(mirrorUpdateAttrDTO);
        JSONObject obj = new JSONObject();
        obj.put("successCount", "成功更新记录：" + integer);
        return obj.toJSONString();
    }

    /**
     * 删除解释字段里多余属性(仅：可新增，可删除)
     * @param mirrorDeleteAttrDTO
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/deleteMirrorExtraAttr", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String deleteMirrorExtraAttr(@RequestBody @Valid MirrorDeleteAttrDTO mirrorDeleteAttrDTO) throws Exception {
        if (!proBaseConfig.getXbbCode().equals(mirrorDeleteAttrDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }

        Integer integer = mirrorConfigService.deleteMirrorExtraAttr(mirrorDeleteAttrDTO);

        JSONObject obj = new JSONObject();
        obj.put("successCount", "成功更新记录：" + integer);
        return obj.toJSONString();
    }

}
